package cn.wei.sdd.core.controller.admin;


import cn.wei.common.exception.Assert;
import cn.wei.common.result.R;
import cn.wei.common.result.ResponseEnum;
import cn.wei.sdd.base.util.JwtUtils;
import cn.wei.sdd.core.pojo.vo.LoginVO;
import cn.wei.sdd.core.pojo.vo.AdminVO;
import cn.wei.sdd.core.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author Wei
 * @since 2021-09-22
 */
@Api(tags = "admin管理")
//@CrossOrigin //跨域
@RestController
@RequestMapping("/admin/core/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;


    @ApiOperation("管理员登录")
    @PostMapping("/login")
    private R login(@RequestBody LoginVO loginVO){
        String mobile = loginVO.getUserName();
        String password = loginVO.getPassword();
        Assert.notEmpty(mobile, ResponseEnum.ACCOUNT_NULL_ERROR);
        Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);
        String token = adminService.login(loginVO);

        return R.ok().data("token", token);
    }

    @ApiOperation("根据token获取管理员信息")
    @GetMapping("/getInfo")
    private R login(HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        AdminVO adminVO = adminService.getInfo(userId);
        return R.ok().data("info", adminVO);
    }


}

